<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Ads extends Admin_Controller {

	function __construct ()
	{
		parent::__construct();		
            $this->load->model('ads_m');
            $this->config->load('adsset');
            $this->config->load('categoryset');
			$this->load->helper('form');
			$this->load->library('myclass');
	}
	
	public function index()
	{
		$data['ads'] = $this->ads_m->get_all_ads();
		$data['web_category'] = $this->ads_m->get_web_category();
		$data['ad_type'] = $this->ads_m->get_ad_type();
        // pp($data);exit;
        // $data['ad_city'] = $this->ads_m->get_city_provence(1);
		$this->load->view('ads/index',$data);
	}
	public function add(){
		$data['web_category'] = $this->ads_m->get_web_category();
		$data['ad_type'] = $this->ads_m->get_ad_type();
		$data['ad_city'] = $this->ads_m->get_city_provence(1);
		// pp($data);exit;
		$this->load->view('ads/form',$data);
	}
	public function edit($ad_id){
		$data['web_category'] = $this->ads_m->get_web_category();
		$data['ad_type'] = $this->ads_m->get_ad_type();
		
		$data['ad_city'] = $this->ads_m->get_city_provence(1);
		$data['ads']= $this->ads_m->get_ads_by_ads_id($ad_id);
		// pp($data);exit;
		$this->load->view('ads/form_eidt',$data);
	}
	public function update(){
		$post = $this->input->post();
        $id = isset($post['ad_id']) ? intval($post['ad_id']) : false;
        $data['ad_city'] = isset($post['ad_city']) ? $post['ad_city'] : "";
        $data['web_category'] = isset($post['web_category']) ? $post['web_category']: false;
        $data['ad_name'] = isset($post['ad_name']) ? $post['ad_name'] : "";
        $data['ad_url'] = isset($post['ad_url']) ? $post['ad_url'] : "";
        $data['ad_type'] = isset($post['ad_type']) ? $post['ad_type'] : false;
        $data['ad_time'] =time();
        // pp($id);exit;
        if ($id) {
            $ads = $this->ads_m->sava_ads($id,$data);
            $new_id = $id;
        } else {
            $ads = $this->ads_m->add_ads($data);
            $new_id = $this->db->insert_id();
        }
        if ($ads) {
           		if($_FILES){
	                $config['upload_path'] = FCPATH.'uploads/ads';
	                $config['allowed_types'] = 'gif|jpg|png|jpeg';
	                $config['encrypt_name'] = false;
			        $config['max_size'] = 3000;
		 			
		 			$config['overwrite'] = true;
		 			$config['file_name'] = $new_id;
	                $pic_path = base_url('uploads/ads/');
	                if(!file_exists($config['upload_path'])){
	                    mkdir($config['upload_path'],0777,true);
	                }
	                $this->load->library('upload', $config);
	                if ($this->upload->do_upload('ad_pic'))
	                {
	                   $upload_data = $this->upload->data();
	                    $data['ad_pic']=$upload_data['file_name'];
	                    $pics=$this->ads_m->update_ad_pic($new_id,$data);
	                }
            	}
            show_message('操作成功',site_url(X_URL.'ads/index'),1);
        } else {
            show_message('操作失败',site_url(X_URL.'ads/edit/'.$id),1);
        }
	}
	public function del($id)
    {
        $this->ads_m->del_ads($id);
        show_message('删除广告',site_url(X_URL.'ads/index'),1);    

    }
    
}

